import pygame
import random
import time
import math
import sys
pygame.init()
WIDTH = 800
HEIGHT = 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('打飞机')
#加载各种图片
icon = pygame.image.load('picture/ufo.ico')
pygame.display.set_icon(icon)
bg = pygame.image.load('picture/bg.jpg')
player = pygame.transform.smoothscale(pygame.image.load('picture/getou.png'), (50, 50))
enemyimg = pygame.transform.smoothscale(pygame.image.load('picture/gege.png'), (50, 50))
bulletimg = pygame.transform.smoothscale(pygame.image.load('picture/basketball.png'), (20, 20))
#加载各种bgm
pygame.mixer.music.load('bgm/bgm1.mp3')
sheji_suond =pygame.mixer.Sound('bgm/ji.MP3')
hit_sound = pygame.mixer.Sound('bgm/niganma.WAV')
clock = pygame.time.Clock()
#全局变量
running = True
is_over = False
playerX = 400
playerY = 500
playerStepX = 0
playerStepY = 0
playercoordinate = None
enemystepY = 50
#设置
choices = input('请选择：\na.10个敌人\nb.15个敌人\nc.20个敌人\nd.5个敌人\n你的选择：')
if choices == 'a':
    num_enemies = 10
    num_bullet = 20
elif choices == 'b':
    num_enemies = 15
    num_bullet = 30
elif choices == 'c':
    num_enemies = 20
    num_bullet = 40
elif choices == 'd':
    num_enemies = 5
    num_bullet = 10
else:
    if choices.isdigit():
        num_enemies = int(choices)
        num_bullet = int(choices)*2
    else:
        print('请输入a,b,c,d中的一个')


fps = 165
score = 0


#敌人类
class Enemy():
    def __init__(self):
        self.img = enemyimg
        self.x = random.randint(50, WIDTH - 50)
        self.y = random.randint(0, 200)
        self.stepX = random.choice([-1.5, -1, -0.5, 0.5, 1, 1.5])
        self.stepY = random.choice([30, 20, 60, 50, 40, 70])


#存放子弹
enemys = []


#子弹类
class Bullet():
    def __init__(self):
        self.img = pygame.image.load('picture/bullet.png')
        self.x = playerX + 17
        self.y = playerY
        self.speed = 3#子弹移动的速度
        self.score = score
    def hit(self):
        global score
        for e in enemys:
            if distance(self.x+10, self.y+10, e.x+25, e.y+25) < 25:#判定
                print('击中')
                bullets.remove(self)
                enemys.remove(e)
                hit_sound.play()
                score += 10
                print('+10')


bullets = []#存子弹的

#创建多个敌人
for i in range(num_enemies):
    enemys.append(Enemy())


#获取键盘事件
def control_player():
    key_left, key_right, key_up, key_down = False, False, False, False
    global playerStepX, playerStepY, running, num_bullet, score
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            pygame.quit()
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                key_left = True
            elif event.key == pygame.K_RIGHT:
                key_right = True
            elif event.key == pygame.K_UP:
                key_up = True
            elif event.key == pygame.K_DOWN:
                key_down = True
            elif event.key == pygame.K_SPACE:
                num_bullet += -1
                if num_bullet > 0:
                    bullets.append(Bullet())
                    sheji_suond.play()
                    print('发射')
                else:
                    num_bullet = 0
                    print('你没有子弹了')
        else:
            playerStepX = 0
            playerStepY = 0
        if key_left:
            playerStepX = -1
            print('左')
        elif key_right:
            playerStepX = 1
            print('右')
        elif key_up:
            playerStepY = -1
            print('上')
        elif key_down:
            playerStepY = 1
            print('下')


#玩家移动
def player_moving():
    global playerX, playerY
    playerX += playerStepX
    playerY += playerStepY
    if playerX > WIDTH-50:
        playerX = WIDTH-50
    elif playerX < 0:
        playerX = 0
    elif playerY > HEIGHT-50:
        playerY = HEIGHT-50
    elif playerY < 0:
        playerY = 0


#子弹显示
def show_bullet():
    for b in bullets:
        screen.blit(bulletimg, (b.x, b.y))
        b.hit()
        b.y -= b.speed
        if b.y < 0:
            bullets.remove(b)


#敌人显示
def show_enmey():
    global is_over, score
    for e in enemys:
        screen.blit(e.img, (e.x, e.y))
        e.x += e.stepX
        if e.x > WIDTH - 50 or e.x < 0:
            e.stepX *= -1
            e.y += e.stepY
            if e.y > HEIGHT - 50:
                gete = input('game over,if you want to shut down immediately, please enter “e”:')
                if gete == 'e':
                    is_over = True
                    enemys.clear()


#判定距离（敌人与子弹）
def distance(ex, ey, bx, by):
    a = ex - bx
    b = ey - by
    return math.sqrt(a**2 + b**2)


#信息显示
def game_msg():
    global playercoordinate
    font = pygame.font.SysFont('Arial', 18)
    playercoordinate = font.render(f'fps:{fps} X:{playerX} Y:{playerY} bullets：{num_bullet} score:{score}', True, (0, 0, 0))
    return playercoordinate


#图片显示
def draw_game():
    screen.blit(bg, (0, 0))
    screen.blit(player, (playerX, playerY))
    screen.blit(game_msg(), (0, 0))
    show_bullet()



pygame.mixer.music.play(-1)#背景音

#游戏主循环
while running:
    draw_game()
    show_enmey()
    control_player()
    player_moving()
    pygame.display.update()
    if is_over or not enemys:
        print('game over')
        break
    clock.tick(fps)